//Borland C++Builder
//Copyright (c) 1987, 1998 Borland International Inc. All Rights Reserved.
//----------------------------------------------------------------------------
//---------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include <stdio.h>
#include <signal.h>
#include "eic.h"
#include "EICVer.h"
#include <dir.h>  //Modifié par DavidR pour le release, MERCI DE LE LAISSER (le 01/04/99 (pendant tes vacances))
#include "uAbout.h"
#include "uRtf.h"

//---------------------------------------------------------------------
#pragma link "CUrlLbl"
#pragma link "CBitBtn"
#pragma resource "*.dfm"
#define MODULE_TAG MODULE_TAG_ABOUT


#define FICHIER_MARQUE "Marque.ini"
char TAboutBox::s_viRootStr[] =             "\\";
char TAboutBox::s_viTransStr[] =            "\\VarFileInfo\\Translation";
char TAboutBox::s_viSubBlockQueryFmt[] =    "\\StringFileInfo\\%08lx\\%s";
char TAboutBox::s_viSpecialBuildStr[] =     "SpecialBuild";

//---------------------------------------------------------------------
__fastcall TAboutBox::TAboutBox(TComponent* AOwner)
	: TForm(AOwner)
{
    m_Buff = 0;
    m_hComment = 0;
    Glob->Rtf->Init();
}
//---------------------------------------------------------------------
__fastcall TAboutBox::~TAboutBox()
{
  if( m_Buff) delete[] m_Buff;
  if( m_hComment) ::DestroyWindow( m_hComment);
  if( m_CaracPartenaire) delete m_CaracPartenaire;
}
//---------------------------------------------------------------------
void __fastcall TAboutBox::OKButtonClick(TObject *Sender)
{
	Close();
}
//---------------------------------------------------------------------------
class MyTCustomForm : public TCustomForm
{
    public :
//        TCustomForm(Classes::TComponent* AOwner):TCustomForm(AOwner);
        __property Icon;
};
void __fastcall TAboutBox::SetInstModule( char *modFName)
{
    ::OemToAnsi(modFName, modFName);

    if (!Init( modFName))
    {
        m_Buff = 0; //!CQ throw?
        return;
    }
    AnsiString s,s1;

//    if( GetFileDescription( s)) FileDescript->Caption = s;
//    if( GetFileVersion( s))     Version->Caption  = "Version " + s;
    if( GetProductName( s))     ProductName->Caption = s;
    if( GetProductVersion(s))   Version->Caption  = "Version " + s;
    if( GetCompagnyName( s))    CompagnyName->Caption = s;
    if( GetUrlSite( s))             {UrlSite->Url = s; UrlSite->Caption = s;}
    else { lblSite->Visible = false;        UrlSite->Visible = false;}
    if( GetUrlCommercial( s, s1))   {UrlCommercial->Url = s; UrlCommercial->Caption = s1;}
    else { LblCommercial->Visible = false;  UrlCommercial->Visible = false;}
    if( GetUrlTechnique( s, s1))    {UrlTechnique->Url = s;  UrlTechnique->Caption = s1;}
    else { lblTechnique->Visible = false;   UrlTechnique->Visible = false;}
    if( GetComments( s))
    {
        Comments->Caption = EmptyStr;
        DWORD Style = ES_MULTILINE | WS_CLIPSIBLINGS | ES_WANTRETURN | WS_VISIBLE | WS_CHILD | ES_CENTER;
        m_hComment = ::CreateWindowEx( WS_EX_TRANSPARENT, RICHEDIT_CLASS, "",
                          Style, Comments->Left, Comments->Top, Comments->Width, Comments->Height,
                          Handle, NULL, 0, NULL);
        LOGFONT lgf;
        ::_GetObject( 1, Canvas->Font->Handle, sizeof( lgf), &lgf);
        ::CHARFORMAT2 charformat;
        memset( &charformat, 0, sizeof( charformat));
        charformat.cbSize           = sizeof( charformat);
        charformat.dwMask           = CFM_SIZE | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT |
                                      CFM_COLOR | CFM_CHARSET | CFM_FACE | CFM_WEIGHT | CFM_BACKCOLOR;// | CFM_SPACING ;
        charformat.yHeight          = Canvas->Font->Size * 20;// * Glob->Resolution / Glob->ResolEcran;
        charformat.crTextColor      = ColorToRGB( Canvas->Font->Color);
        charformat.crBackColor      = ColorToRGB( clWindow);
        charformat.bCharSet         = lgf.lfCharSet;
        charformat.bPitchAndFamily  = lgf.lfPitchAndFamily;
        strcpy( charformat.szFaceName, Canvas->Font->Name.c_str());
        charformat.wWeight          = (WORD)lgf.lfWeight;
        charformat.dwEffects        = (lgf.lfItalic?CFE_ITALIC:0)
                                    | (lgf.lfUnderline?CFE_UNDERLINE:0)
                                    | (lgf.lfStrikeOut ?CFE_STRIKEOUT:0);
        ::SendMessage( m_hComment, EM_SETCHARFORMAT , SCF_ALL, (long)&charformat);
        ::SendMessage( m_hComment, EM_AUTOURLDETECT, TRUE, 0 );
        Style = ::SendMessage( m_hComment, EM_GETEVENTMASK, 0, 0);
        ::SendMessage( m_hComment, EM_SETEVENTMASK, 0, Style | ENM_LINK);
        ::SetWindowText( m_hComment, s.c_str());
    }
    TCustomForm *f = dynamic_cast<TCustomForm*>(Owner);
    TIcon *i;
    if( f) i = ((MyTCustomForm*)f)->Icon;
    else   i = Application->Icon;
    ProgramIcon->Picture->Bitmap = new Graphics::TBitmap();
    ProgramIcon->Picture->Bitmap->Height = i->Height;
    ProgramIcon->Picture->Bitmap->Width = i->Width;
    ProgramIcon->Picture->Bitmap->Canvas->Brush->Color = clFuchsia;
    ProgramIcon->Picture->Bitmap->Canvas->FillRect( ProgramIcon->Picture->Bitmap->Canvas->ClipRect);
    ProgramIcon->Picture->Bitmap->Canvas->Draw( 0,0,i);

    ProgramIcon->Picture->Bitmap->TransparentMode = tmFixed;
    ProgramIcon->Picture->Bitmap->TransparentColor= clFuchsia;
}


//
// Internal initialization fills in the data members
//
bool __fastcall TAboutBox::Init(const char* modFName)
{
    unsigned long fvHandle;
    unsigned int  infoSize = ::GetFileVersionInfoSize((char *)modFName, &fvHandle);
    if (!infoSize)
    {  //!JK - an exception seems better here (chgd from return false)
        AfficheLastError();
        return false;
    }

    m_Buff  = new char[ infoSize];
    if (!::GetFileVersionInfo((char *)modFName, fvHandle, infoSize, m_Buff))
        return false;

    // Copy string to buffer so if the -dc compiler switch (Put constant
    // strings in code segments) is on, VerQueryValue will work under Win16.
    // This works around a problem in Microsoft's ver.dll which writes to the
    // string pointed to by subBlockName.
    //
    unsigned int  vInfoLen;                 // not really used, but must be passed
    if (!::VerQueryValue( m_Buff, s_viRootStr, (void **)&m_FixedInfo, &vInfoLen))
        return false;

    unsigned int * trans;
    if (!::VerQueryValue( m_Buff, s_viTransStr, (void **)&trans, &vInfoLen))
        return false;

    // Swap the words so sprintf will print the lang-charset in the correct
    // format.
    //
    m_Lang = MkUINT(HiUSHORT(*trans), LoUSHORT(*trans));

    if( FileExists( FICHIER_MARQUE))
    {
        m_CaracPartenaire = new TIniFile( IncludeTrailingBackslash(ExtractFilePath(modFName))+ FICHIER_MARQUE);
    }
    return true;
}
//
// Query any given "\StringFileInfo\lang-charset\<str>" version info string
// lang indicates the language translation, may be 0 to signify file default.
//
bool __fastcall TAboutBox::GetInfoString( const AnsiString &str, AnsiString& Value, unsigned int lang)
{
    unsigned int vSize;
    char         subBlockName[255], *szValue;
    if( m_Buff)
    {
        sprintf( subBlockName, s_viSubBlockQueryFmt, (lang? lang : m_Lang),  str.c_str() );
        if( ::VerQueryValue(m_Buff, subBlockName, (void **)&szValue, &vSize))
        {
            Value = szValue;
            return true;
        }
    }
    return false;
}

//
// Commonly used, predefined info string queries. Pass requested language thru,
// may be 0 to signify default.
//
bool __fastcall TAboutBox::GetFileDescription(AnsiString& fileDesc, unsigned int lang)
{
    return GetInfoString(_KEY_FILEDESCRIPTION, fileDesc, lang);
}

//
// Retrieve the file version information in the requested language id.
//
bool __fastcall TAboutBox::GetFileVersion(AnsiString& fileVersion, unsigned int lang)
{
    return GetInfoString(_KEY_FILEVERSION, fileVersion, lang);
}

//
// Retrieve the internal name of the module.
//
bool __fastcall TAboutBox::GetInternalName(AnsiString& internalName, unsigned int lang)
{
    return GetInfoString(_KEY_INTERNALNAME, internalName, lang);
}

//
// Retrieve the copyright message.
//
bool __fastcall TAboutBox::GetLegalCopyright(AnsiString& copyright, unsigned int lang)
{
    return GetInfoString(_KEY_LEGALCOPYRIGHT, copyright, lang);
}

//
// Retrieve the original filename.
//
bool __fastcall TAboutBox::GetOriginalFilename(AnsiString& originalFilename, unsigned int lang)
{
    return GetInfoString(_KEY_ORIGINALFILENAME, originalFilename, lang);
}

//
// Retrieve the product name this module is associated with.
//
bool __fastcall TAboutBox::GetProductName(AnsiString& prodName, unsigned int lang)
{
    return GetInfoString(_KEY_PRODUCTNAME, prodName, lang);
}
//
// Retrieve the comments this module is associated with.
//
bool __fastcall TAboutBox::GetComments(AnsiString& Comment, unsigned int lang)
{
    return GetInfoString(_KEY_COMMENTS, Comment, lang);
}
//
// Retrieve the compagny Name this module is associated with.
//
bool __fastcall TAboutBox::GetCompagnyName(AnsiString& CompagnyName, unsigned int lang)
{
    if( m_CaracPartenaire)
    {
        CompagnyName = m_CaracPartenaire->ReadString("Coordonnees", _KEY_COMPANYNAME, EmptyStr);
        return !CompagnyName.IsEmpty();
    }
    return GetInfoString(_KEY_COMPANYNAME, CompagnyName, lang);
}
bool __fastcall TAboutBox::GetUrlSite(          AnsiString& Url,            unsigned int lang)
{
    if( m_CaracPartenaire)
    {
        Url = m_CaracPartenaire->ReadString("Coordonnees", _KEY_SITE_INTERNET, EmptyStr);
        return !Url.IsEmpty();
    }
    return GetInfoString(_KEY_SITE_INTERNET, Url, lang);
}
bool __fastcall TAboutBox::GetUrl(    AnsiString& Url, AnsiString& Mail, bool bCommercial, unsigned int lang)
{
    bool bReturn = false;
    if( m_CaracPartenaire)
    {
        Mail = m_CaracPartenaire->ReadString("Coordonnees", bCommercial?_KEY_EMAIL_CO:_KEY_EMAIL_TECH, EmptyStr);
        bReturn = !Mail.IsEmpty();
    }else
        bReturn = GetInfoString(bCommercial?_KEY_EMAIL_CO:_KEY_EMAIL_TECH, Mail, lang);
    if( bReturn) Url = AnsiString( "MailTo:") + Mail;
    return bReturn;
}
//
// Retrieve the version of the product.
//
bool __fastcall TAboutBox::GetProductVersion(AnsiString& prodVersion, unsigned int lang)
{
    return GetInfoString(_KEY_PRODUCTVERSION, prodVersion, lang);
}

//
// Retrieve the special build number.
//
bool __fastcall TAboutBox::GetSpecialBuild(AnsiString& specialBuild, unsigned int lang)
{
    //!CQ could check VS_FF_SPECIALBUILD flag first...
    return GetInfoString(s_viSpecialBuildStr, specialBuild, lang);
}

//
// Get the language name string associated with a language/charset code
//
AnsiString __fastcall TAboutBox::GetLanguageName(unsigned int lang)
{
    char langStr[128];  *langStr = '\0';
    ::VerLanguageName( lang, langStr, sizeof( langStr));
    return AnsiString( langStr);
}

// returns values of TFileType if GetFileType() returned
// DevDriver or Font.
// If GetFileType() returned VirtDriver, this function returns the virtual
// device inentifier included in the virtual device control block.
unsigned int __fastcall TAboutBox::GetFileSubType() const
{
    unsigned int fileSubType = m_FixedInfo->dwFileSubtype;

    switch (GetFileType())
    {
        case DevDriver:
                switch ( m_FixedInfo->dwFileSubtype)
                {
                    case VFT2_DRV_PRINTER:      fileSubType = PtrDriver;          break;
                    case VFT2_DRV_KEYBOARD:     fileSubType = KybdDriver;         break;
                    case VFT2_DRV_LANGUAGE:     fileSubType = LangDriver;         break;
                    case VFT2_DRV_DISPLAY:      fileSubType = DisplayDriver;      break;
                    case VFT2_DRV_MOUSE:        fileSubType = MouseDriver;        break;
                    case VFT2_DRV_NETWORK:      fileSubType = NtwkDriver;         break;
                    case VFT2_DRV_SYSTEM:       fileSubType = SysDriver;          break;
                    case VFT2_DRV_INSTALLABLE:  fileSubType = InstallableDriver;  break;
                    case VFT2_DRV_SOUND:        fileSubType = SoundDriver;        break;
                    case VFT2_UNKNOWN: default: fileSubType = UnknownDevDriver;   break;
                }
            break;
        case Font:
                switch (m_FixedInfo->dwFileSubtype)
                {
                    case VFT2_FONT_RASTER:      fileSubType = RasterFont;         break;
                    case VFT2_FONT_VECTOR:      fileSubType = VectorFont;         break;
                    case VFT2_FONT_TRUETYPE:    fileSubType = TrueTypeFont;       break;
                    case VFT2_UNKNOWN: default: fileSubType = UnknownFont;        break;
                }
            break;
    }
    return fileSubType;
}

//
//
//
FILETIME __fastcall TAboutBox::GetFileDate() const
{
    FILETIME ft = {m_FixedInfo->dwFileDateLS, m_FixedInfo->dwFileDateMS};
    return ft;
}

void __fastcall EditAboutBox(TComponent* AOwner, const AnsiString &Name)
{
    TAboutBox *AboutBox = new TAboutBox( AOwner);
    AboutBox->SetInstModule( Name.c_str());
    AboutBox->ShowModal();
    delete AboutBox;
}

void __fastcall TAboutBox::WMNotify( TWMNotify &Msg)
{
    if( Msg.NMHdr->code == EN_LINK)
    {
        ENLINK *l = (ENLINK*)Msg.NMHdr;
        if( l->msg == WM_LBUTTONDOWN)
        {
            CHARFORMAT c;
            char *Url = (char*)::_GlobalAlloc( TAGL, GPTR, l->chrg.cpMax - l->chrg.cpMin +1);
            ::SendMessage( m_hComment, EM_EXGETSEL, 0, (LPARAM)&c);
            ::SendMessage( m_hComment, EM_EXSETSEL, 0, (LPARAM)&l->chrg);
            ::SendMessage( m_hComment, EM_GETSELTEXT, 0, (LPARAM)Url);
            ::SendMessage( m_hComment, EM_EXSETSEL, 0, (LPARAM)&c);
            ::ShellExecute( NULL, "open", Url, NULL, NULL, SW_SHOWNORMAL);
            ::_GlobalFree( TAGL, Url);
        }
    }
}

